home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0090_Convert REAL to Float.pas < prev   
Pascal/Delphi Source File  |  1995-02-28  |  1KB  |  52 lines

  1. Program RealConv;
  2.  
  3. {  This program demonstrates the conversion of a 6 byte Turbo Pascal   }
  4. {  real variable type by re-creating the exponent and Mantissa.        }
  5. {  The temporary variable Mantissa accumulates the value stored in     }
  6. {  bytes 2 through 6.                                                  }
  7.  
  8. type
  9.   SixByteArray = array[1..6] of byte;
  10.  
  11. var
  12.   r : real;
  13.   s : SixByteArray absolute r;
  14. { Allows access to individual real type bytes }
  15.   i,j : byte;
  16.   PosFlag : boolean;
  17.   Mantissa : real;
  18.   Number : real;
  19.  
  20. function power (x,y : integer) : real;
  21. begin
  22.   power := exp(y * ln(x));
  23. end;
  24.  
  25. begin
  26.   write('Enter floating point Number ');
  27.   readln(r);
  28. { Check if entry is positive from bit 7 of byte 6 }
  29.   PosFlag := ($80 and s[6]) = 0; 
  30. { Force bit 7 of byte 6 on }
  31.   s[6] := s[6] or $80;           
  32. { Initialize the Mantissa }
  33.   Mantissa := 1.0;               
  34. { Check each byte of mantissa }
  35.   for i := 2 to 6 do             
  36. { Check each bit }
  37.     for j := 0 to 7 do           
  38.       if ((s[i] shr j) and 1 ) = 1 then
  39. { Increment mantissa appropriately }
  40.         Mantissa := Mantissa + power(2, (j + (i-2)*8));
  41.  
  42. { Normalize the number by dividing by 2^40 }
  43.   Number := Mantissa / power(2,40);
  44.  
  45. { Get number by multiply Mantissa by the exponent }
  46.   Number := Number * power(2, s[1] - $80);  
  47.   if not PosFlag then Number := Number * -1;
  48.   writeln(Number);
  49.   readln;
  50. end.
  51.  
  52.